package com.boge.utils.listener;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.boge.study.test.websocket.WebsocketEndPoint;

public class SessionListener implements HttpSessionListener {
	
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		System.out.println("sessionCreated---");
//		HttpSession session = event.getSession();
//		System.out.println("session:" + session);
//		synchronized (session) {
//			WebsocketEndPoint.onlineNum ++;	
//			new WebsocketEndPoint().sendMessageToAllUsers(new TextMessage(WebsocketEndPoint.onlineNum + ""));
//		}
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		System.out.println("sessionDestroyed---");
		HttpSession session = event.getSession();
		System.out.println("userId:" + session.getAttribute("userId"));
		if (session.getAttribute("userId") != null) {
			synchronized (session) {
//				WebsocketEndPoint.onlineNum --;
				WebsocketEndPoint.onlineUserMap.remove(session.getAttribute("userId"));
				System.out.println("onlineNum:" + WebsocketEndPoint.onlineUserMap.size());
				new WebsocketEndPoint().sendMessageToAllUsers(new TextMessage(WebsocketEndPoint.onlineUserMap.size() + ""));
//				System.out.println("------"); 也可自动跳转登录界面
			}
		}
	}

}
